<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="courseArr" border style="width: 100%">
      <el-table-column prop="id" label="课程编号" width="100"></el-table-column>
      <el-table-column prop="name" label="课程名称"></el-table-column>
      <el-table-column prop="dept" label="所属系"></el-table-column>
      <el-table-column prop="year" label="年级"></el-table-column>
      <el-table-column prop="teacher" label="教师"></el-table-column>
      <el-table-column prop="times" label="上课时间"></el-table-column>
      <el-table-column prop="loc" label="上课地点"></el-table-column>
      <el-table-column prop="credit" label="学分" width="100"></el-table-column>
      <el-table-column prop="type" label="课程类型"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="success"
                     @click="handleEdit(scope.row)">修改
          </el-button>&nbsp;
          <el-button size="mini" type="danger"
                     @click="openDeleteConfirm(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" style="margin-top: 30px" @click="goBack()">返回</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      wd:"",
      courseArr: [],
    };
  },
  methods: {
    goBack(){
      this.$router.push('/sys-admin/main/course/list');
    },
    loadCourseDetail() {
      console.log('loadCourseDetail')
      let url = 'http://localhost:9800/course/selectByWd'+location.search;
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this. courseArr= responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    search(){
      let url = 'http://localhost:9800/course/selectByWd' + this.wd;
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.courseArr = responseBody.data
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    handleEdit(course) {
      this.$router.push('update?id='+course.id);
    },
    handleDelete(course) {
      let url = 'http://localhost:9800/course/'+ course.id +'/delete'
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if(responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        this.$router.push('/sys-admin/main/course/list');
      })
    },
    openDeleteConfirm(course){
      let message = '此操作将永久删除【'+ course.name+'】课程，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(course);
      }).catch(() => {

      });
    },
  },
  mounted() {
    this.loadCourseDetail();
  }
}
</script>